package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.ProcessOrderPaidBean;
import com.ymatou.iapi.livebuyer.service.ProcessOrderPaidCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.ModifyLiveActivityBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ActivityCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*接收库存变更通知
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ProcessOrderPaid{
	private static ProcessOrderPaidBean ProcessOrderPaidBean;
	private static ProcessOrderPaidCall ProcessOrderPaidCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("处理订单支付消息");
	}

	@Before
	public void caseUp() {
		ProcessOrderPaidBean=new ProcessOrderPaidBean();
		ProcessOrderPaidCall=new ProcessOrderPaidCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(ProcessOrderPaidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProcessOrderPaid_001() {
		//需要时间一致
		Logger.start(true,"处理订单支付消息成功");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService
					.initUserRegisterInfoRequestDtoBean();
			
			UserRegisterInfoRequestDtoCall uc = JUserCallService
					.UserRegisterInfoRequestDtoCall(urb);

			Integer uid = uc.getUserid();
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 10s后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			// 半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			Logger.debug("starttime:"+starttime.toString());
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			createliveactivityBean.getLiveActivity().setEndTime(String.valueOf(starttime.getTime()+20*60*1000));
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			ModifyLiveActivityBean ModifyLiveActivityBean = LiveRoomCallService.initModifyLiveActivityBean(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Thread.sleep(20000);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			ProcessOrderPaidBean.setRequestId(reqid);
			ProcessOrderPaidBean.setBuyerId(uid);
			ProcessOrderPaidBean.setProductIds(Arrays.asList(productId));
			ProcessOrderPaidCall.setData(ProcessOrderPaidBean);
			ProcessOrderPaidCall.callService();	
			Logger.verifyEquals("ok", ProcessOrderPaidCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains(ProcessOrderPaidBean.getRequestId().toString())).filter(s->s.contains("request body")).collect(Collectors.toList());
			Logger.debug(filter);
			//20200226  恭喜%s 抢到了%d号商品  这条消息产品不发了 by王旭东
			Logger.verifyEquals(0,filter.size(),"验证日志");
			//xxx Thread:main 2020-01-10 16:02:10.955 2020-01-10 16:02:07,984 [http-nio-9062-exec-4] INFO  c.y.l.infrastructure.util.HttpClientUtil KEY[ProcessOrderPaidReq|82fdd645929746a9be4244e6b73f5a97] [782e8d2b0c7cb78b] - request body: {"Content":"{\"BizData\":{\"Content\":\"恭喜atsLI31QzoDG 抢到了1号商品\",\"LiveId\":211941},\"BizType\":605}","GroupId":"1578643290524"}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}